package com.utils;

import sun.misc.Cleaner;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Utils {
    /**
     * 生成文件路径
     * @return
     */
    public static String createFilePath(String baseUploadPath,Integer userId){
//        String filePath = "C:/Users/Administrator/Desktop/file/";
//        File file = new File(filePath);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate now = LocalDate.now();
        String format = formatter.format(now);

        String datePath =  format  + "/" + userId + "/";
        File fullFile = new File(baseUploadPath  + datePath);
        if (!fullFile.exists()) {
            fullFile.mkdirs();
        }
        return datePath ;
    }


    /**
     * 关闭map
     * @param map
     */
     public static void cleanBuffer(MappedByteBuffer map) {
        try {
            Method getCleanerMethod = map.getClass().getMethod("cleaner");
            Cleaner.create(map, null);
            getCleanerMethod.setAccessible(true);
            Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(map);
            cleaner.clean();
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }



}
